package com.loongtech.bi.action.game;

import com.loongtech.bi.action.BaseBiSearchAction;
import com.loongtech.bi.action.PageHelper;
import com.loongtech.bi.manager.game.*;
import com.loongtech.bi.manager.game.EconomyGameManager.YuanbaoConsumeData;
import com.loongtech.bi.manager.game.EconomyGameManager.YuanbaoData;
import com.loongtech.bi.manager.game.EconomyGameManager.YuanbaoOutputData;
import com.loongtech.bi.manager.system.GameParamConfigManager;
import com.loongtech.bi.manager.system.SysLogManager;
import com.loongtech.bi.support.CommonGameParam;
import com.loongtech.bi.support.Session;
import com.loongtech.bi.support.chart.ChartTool;
import com.loongtech.bi.support.chart.ChartType;
import com.loongtech.bi.support.chart.ChartUIData;
import com.loongtech.core.util.NumberUtil;
import com.loongtech.core.util.Time;
import org.datanucleus.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import java.util.*;

/**
 * 游戏内分析-经济分析Action
 * @author Marin
 * @date 2015年09月17日
 */
@Controller
@Scope("view")
public class EconomyGameAction extends BaseBiSearchAction {
	private static final long serialVersionUID = 1L;
	private List<YuanbaoData> listYuanbaoData; //元宝数据列表
	private ChartUIData yuanbaoChart;  // 前端引用的元宝图表数据
	
	private List<YuanbaoOutputData> yuanbaoOutputData;  // 元宝产出途径数据
	private ChartUIData yuanbaoOutputChart;  // 元宝产出途径图表
	
	private List<YuanbaoConsumeData> yuanbaoConsumeData;  // 元宝消耗途径数据
	private ChartUIData yuanbaoConsumeChart;  // 元宝消耗途径图表

    private String queryCurType;

    private List<EconomyGameManager.YuanbaoSumData> yuanbaoSumDatas;
    private ChartUIData yuanbaoSumChart;

    private List<EconomyGameManager.YuanbaoConsumeData> shangchengConsumeDatas;
    private ChartUIData shangchengConsumeChart;
	
	@Autowired
	private EconomyGameManager economyGameManager;
	@Autowired
    private MallStatisticsAllManager mallStatisticsAllManager;
	@Autowired
    private MallStatisticsAllNoParamManager mallStatisticsAllNoParamManager;


	@Autowired
	private SysLogManager sysLogManager;
	@Autowired
	private Session session;

    private Map<String, String> yuabaoItmes = GameParamConfigManager.getItems(CommonGameParam.ECONOMY_YUAN_BAO_ID);
	private Map<String, String> yuabaoItmesRevise = GameParamConfigManager.getItemsRevise(CommonGameParam.ECONOMY_YUAN_BAO_ID);
    private Map<String, String> rmbItmesRevise = GameParamConfigManager.getItemsRevise(CommonGameParam.RMB_REASON);  // 元宝产出和消耗途径反转

	@PostConstruct
	public void init() {
        yuabaoItmes.remove("全部经济统计元宝ID");
        yuabaoItmesRevise.remove("-99999");

        for (String s : yuabaoItmes.values()) {
            queryCurType = s;
            break;
        }

		search();
	}

	public void search() {
		sysLogManager.addLog(session.getUserName(), "游戏内分析-经济分析");

        FacesContext context = FacesContext.getCurrentInstance();
        String shangchengReason = (String) GameParamConfigManager.getItemsRevise(CommonGameParam.SHANGCHENG_REASON).keySet().toArray()[1];
        if (StringUtils.isEmpty(shangchengReason) || GameParamConfigManager.ALL_FLAT.equals(shangchengReason)) {
            context.addMessage(null, new FacesMessage("提示", "没有配置 64 消耗原因是商城 的值 !"));
            return;
        }

		yuanbaoChart = null;
		yuanbaoOutputChart = null;
		yuanbaoConsumeChart = null;

        yuanbaoSumChart = null;

		//元宝数据列表
		listYuanbaoData = economyGameManager.getListYuanbaoData(dateStart, dateEnd, queryPlatform, queryChannel, queryServer,queryCurType);
		
		if (listYuanbaoData != null && listYuanbaoData.size() > 0) {
			yuanbaoChart = new ChartUIData();
			yuanbaoChart.setJsonType(ChartType.SPLINE.type);
			yuanbaoChart.setJsonStr(new ChartTool(listYuanbaoData, YuanbaoData.class, yuanbaoChart.getSelectItem()).loadData(ChartType.SPLINE.type));
			yuanbaoChart.setAllItmes(ChartTool.getAllItems(YuanbaoData.class));
		}
		
		//产出数据
		yuanbaoOutputData = economyGameManager.getListYuanbaoOutputData(dateStart, dateEnd, queryPlatform, queryChannel, queryServer,queryCurType);
		if (yuanbaoOutputData != null && yuanbaoOutputData.size() > 0) {
			yuanbaoOutputChart = new ChartUIData();
			yuanbaoOutputChart.setJsonType(ChartType.BAR2.type);
			yuanbaoOutputChart.setJsonStr(new ChartTool(yuanbaoOutputData, YuanbaoOutputData.class, yuanbaoOutputChart.getSelectItem()).loadData(ChartType.BAR.type));
			yuanbaoOutputChart.setAllItmes(ChartTool.getAllItems(YuanbaoOutputData.class));
		}
		
		//消耗数据
		yuanbaoConsumeData = economyGameManager.getListYuanbaoConsumeData(dateStart, dateEnd, queryPlatform, queryChannel, queryServer, queryCurType);
		if (yuanbaoConsumeData != null && yuanbaoConsumeData.size() > 0) {
			yuanbaoConsumeChart = new ChartUIData();
			yuanbaoConsumeChart.setJsonType(ChartType.BAR2.type);
            yuanbaoConsumeChart.setAllItmes(ChartTool.getAllItems(YuanbaoConsumeData.class));
            if ("consumeUser".equals(yuanbaoConsumeChart.getSelectItem())) {
                Collections.sort(yuanbaoConsumeData, economyGameManager.new YuanbaoConsumeData().comparatorConsumeUser);
            } else {
                Collections.sort(yuanbaoConsumeData, economyGameManager.new YuanbaoConsumeData().comparatorConsumeCount);
            }
            List<YuanbaoConsumeData> ll = yuanbaoConsumeData.size() > 20 ? yuanbaoConsumeData.subList(0, 20) : yuanbaoConsumeData;
            Collections.reverse(ll);
			yuanbaoConsumeChart.setJsonStr(new ChartTool(ll, YuanbaoConsumeData.class, yuanbaoConsumeChart.getSelectItem()).loadData(ChartType.BAR.type));
		}

        yuanbaoSumDatas = economyGameManager.getYuanbaoSumDatas(dateStart, dateEnd, queryPlatform, queryChannel, queryServer,queryCurType);
        if (yuanbaoSumDatas != null && yuanbaoSumDatas.size() > 0) {
            yuanbaoSumChart = new ChartUIData();
            yuanbaoSumChart.setJsonStr(ChartType.SPLINE.type);
            yuanbaoSumChart.setJsonStr(new ChartTool(yuanbaoSumDatas, EconomyGameManager.YuanbaoSumData.class, "").loadData(yuanbaoSumChart.getJsonType()));
        }

        Map<String, EconomyGameManager.YuanbaoConsumeData> tmp = new LinkedHashMap<>();

        List<Object[]> mallStatisticsAllList = mallStatisticsAllManager.getMallStatisticsAllByAllKind(dateStart, dateEnd, queryPlatform, queryChannel, queryServer, "(" + queryCurType + ")", shangchengReason);
        for(Object[] o : mallStatisticsAllList) {
            String way = NumberUtil.parseLong(o[2]) + "";
            YuanbaoConsumeData e = tmp.get(way);
            if (e == null) {
                e = economyGameManager.new YuanbaoConsumeData();
                e.setConsumeWay(way);
                e.setConsumeWayStr(GameParamConfigManager.getItemsRevise(CommonGameParam.ITEM_TYPE).get(way) == null ? way : GameParamConfigManager.getItemsRevise(CommonGameParam.ITEM_TYPE).get(way));

                tmp.put(way, e);
            }

            e.setConsumeCount(e.getConsumeCount() - NumberUtil.parseLong(o[3]));
            e.setConsumeUser(e.getConsumeUser() + NumberUtil.parseInt(o[5]));
        }
        if (tmp.size() > 0) {
            shangchengConsumeDatas = new ArrayList<>();
            shangchengConsumeDatas.addAll(tmp.values());

            List<Object[]> list = mallStatisticsAllNoParamManager.getMallStatisticsAllByAllKind(Time.getDateStr(dateStart), Time.getDateStr(dateEnd), queryPlatform, queryChannel, queryServer, "(" + queryCurType + ")", shangchengReason);
            long allConsume = 0;
            long allUser = 0;
            for(Object[] o1 : list) {
                allConsume = NumberUtil.parseLong(o1[0]);
                allUser = NumberUtil.parseLong(o1[1]);
            }
            for (YuanbaoConsumeData e : shangchengConsumeDatas) {
                e.setConsumeCountRadio(allConsume == 0 ? 0 : (float)(e.getConsumeCount() * -1.0 / allConsume));
                e.setConsumeUserRadio(allUser == 0 ? 0 : (float)(e.getConsumeUser() * 1.0 / allUser));
            }

            shangchengConsumeChart = new ChartUIData();
            shangchengConsumeChart.setJsonType(ChartType.BAR2.type);
            shangchengConsumeChart.setAllItmes(ChartTool.getAllItems(YuanbaoConsumeData.class));
            if ("consumeUser".equals(shangchengConsumeChart.getSelectItem())) {
                Collections.sort(shangchengConsumeDatas, economyGameManager.new YuanbaoConsumeData().comparatorConsumeUser);
            } else {
                Collections.sort(shangchengConsumeDatas, economyGameManager.new YuanbaoConsumeData().comparatorConsumeCount);
            }
            List<YuanbaoConsumeData> ll = shangchengConsumeDatas.size() > 20 ? shangchengConsumeDatas.subList(0, 20) : shangchengConsumeDatas;
            Collections.reverse(ll);
            shangchengConsumeChart.setJsonStr(new ChartTool(ll, YuanbaoConsumeData.class, shangchengConsumeChart.getSelectItem()).loadData(ChartType.BAR.type));
        } else {
            shangchengConsumeDatas = null;
            shangchengConsumeChart = null;
        }

        /*Map<String, List<EconomyGameManager.YuanbaoConsumeData>> tmp = new LinkedHashMap<>();
        for (EconomyGameManager.YuanbaoConsumeData e : yuanbaoConsumeData) {
            String way = e.getConsumeWay();
            List<YuanbaoConsumeData> ll = tmp.get(way);
            if (ll == null) {
                ll = new ArrayList<>();
                tmp.put(way, ll);
            }

            ll.add(e);
        }
        if (tmp.size() > 0) {
            List<Long> currencyConsumeList = activeUserCurrencyConsumeManager.getSumData(dateStart, dateEnd, queryPlatform, queryChannel, queryServer,queryCurType);
            List<EntityActiveUserCurrencyConsumeUsernum> currencyConsumeUserList = activeUserCurrencyConsumeUsernumManager.getData(dateStart, dateEnd, queryPlatform, queryChannel, queryServer, queryCurType);

            shangchengConsumeDatas = new ArrayList<>();
            for (Map.Entry<String, List<EconomyGameManager.YuanbaoConsumeData>> entry : tmp.entrySet()) {
                String way = entry.getKey();
                List<YuanbaoConsumeData> list = entry.getValue();
                EconomyGameManager.YuanbaoConsumeData e = economyGameManager.new YuanbaoConsumeData();
                e.setConsumeWayStr(GameParamConfigManager.getItemsRevise(CommonGameParam.MALL_NAME).get(way) == null ? "未知商城" : GameParamConfigManager.getItemsRevise(CommonGameParam.MALL_NAME).get(way));

                for (YuanbaoConsumeData consumeData : list) {
                    e.setConsumeCount(e.getConsumeCount() + consumeData.getConsumeCount());
                    e.setConsumeUser(e.getConsumeUser() + consumeData.getConsumeUser());
                }

                if(currencyConsumeList.size() > 0) {
                    Float outputCountRadio = e.getConsumeCount() / currencyConsumeList.get(0).floatValue();
                    e.setConsumeCountRadio(outputCountRadio);
                }

                //人数占比
                if(currencyConsumeUserList.size() > 0) {
                    Float outputUserRadio = e.getConsumeUser() / currencyConsumeUserList.get(0).getUsernum().floatValue();
                    e.setConsumeUserRadio(outputUserRadio);
                }

                shangchengConsumeDatas.add(e);
            }

            if (shangchengConsumeDatas != null && shangchengConsumeDatas.size() > 0) {
                shangchengConsumeChart = new ChartUIData();
                shangchengConsumeChart.setJsonType(ChartType.BAR2.type);
                shangchengConsumeChart.setJsonStr(new ChartTool(shangchengConsumeDatas, YuanbaoConsumeData.class, shangchengConsumeChart.getSelectItem()).loadData(ChartType.BAR.type));
                shangchengConsumeChart.setAllItmes(ChartTool.getAllItems(YuanbaoConsumeData.class));
            }
        } else {
            shangchengConsumeDatas = null;
        }*/
	}

	//获得百分比
	public String getDouble2Percent(Double d) {
		return PageHelper.double2Percent(d);
	}
	
	// 响应前端曲线图改变选择项事件
	public void handleYuanbaoChange() throws Exception {
		yuanbaoChart.setJsonStr(new ChartTool(listYuanbaoData, YuanbaoData.class, yuanbaoChart.getSelectItem()).loadData(ChartType.SPLINE.type));
	}
	
	// 响应前端曲线图改变选择项事件
	public void handleYuanbaoOutputChange() throws Exception {
		yuanbaoOutputChart.setJsonStr(new ChartTool(yuanbaoOutputData, YuanbaoOutputData.class, yuanbaoOutputChart.getSelectItem()).loadData(ChartType.BAR.type));
	}
	
	// 响应前端曲线图改变选择项事件
	public void handleYuanbaoConsumeChange() throws Exception {
        if ("consumeUser".equals(yuanbaoConsumeChart.getSelectItem())) {
            Collections.sort(yuanbaoConsumeData, economyGameManager.new YuanbaoConsumeData().comparatorConsumeUser);
        } else {
            Collections.sort(yuanbaoConsumeData, economyGameManager.new YuanbaoConsumeData().comparatorConsumeCount);
        }
        List<YuanbaoConsumeData> ll = yuanbaoConsumeData.size() > 20 ? yuanbaoConsumeData.subList(0, 20) : yuanbaoConsumeData;
        Collections.reverse(ll);
        yuanbaoConsumeChart.setJsonStr(new ChartTool(ll, YuanbaoConsumeData.class, yuanbaoConsumeChart.getSelectItem()).loadData(ChartType.BAR.type));
	}

    public void handleYuanbaoConsumeChange1() throws Exception {
        if ("consumeUser".equals(shangchengConsumeChart.getSelectItem())) {
            Collections.sort(shangchengConsumeDatas, economyGameManager.new YuanbaoConsumeData().comparatorConsumeUser);
        } else {
            Collections.sort(shangchengConsumeDatas, economyGameManager.new YuanbaoConsumeData().comparatorConsumeCount);
        }

        List<YuanbaoConsumeData> ll = shangchengConsumeDatas.size() > 20 ? shangchengConsumeDatas.subList(0, 20) : shangchengConsumeDatas;
        Collections.reverse(ll);
        shangchengConsumeChart.setJsonStr(new ChartTool(ll, YuanbaoConsumeData.class, shangchengConsumeChart.getSelectItem()).loadData(ChartType.BAR.type));
    }
	
	@Override
	protected String csvContent() {
		return "经济分布";
	}

	@Override
    protected String csvFileName() {
	    return "经济分布";
    }

	public List<YuanbaoData> getListYuanbaoData() {
		return listYuanbaoData;
	}

	public void setListYuanbaoData(List<YuanbaoData> listYuanbaoData) {
		this.listYuanbaoData = listYuanbaoData;
	}

	public ChartUIData getYuanbaoChart() {
		return yuanbaoChart;
	}

	public void setYuanbaoChart(ChartUIData yuanbaoChart) {
		this.yuanbaoChart = yuanbaoChart;
	}

	public List<YuanbaoOutputData> getYuanbaoOutputData() {
		return yuanbaoOutputData;
	}

	public void setYuanbaoOutputData(List<YuanbaoOutputData> yuanbaoOutputData) {
		this.yuanbaoOutputData = yuanbaoOutputData;
	}

	public ChartUIData getYuanbaoOutputChart() {
		return yuanbaoOutputChart;
	}

	public void setYuanbaoOutputChart(ChartUIData yuanbaoOutputChart) {
		this.yuanbaoOutputChart = yuanbaoOutputChart;
	}

	public List<YuanbaoConsumeData> getYuanbaoConsumeData() {
		return yuanbaoConsumeData;
	}

	public void setYuanbaoConsumeData(List<YuanbaoConsumeData> yuanbaoConsumeData) {
		this.yuanbaoConsumeData = yuanbaoConsumeData;
	}

	public ChartUIData getYuanbaoConsumeChart() {
		return yuanbaoConsumeChart;
	}

	public void setYuanbaoConsumeChart(ChartUIData yuanbaoConsumeChart) {
		this.yuanbaoConsumeChart = yuanbaoConsumeChart;
	}

    public String getQueryCurType() {
        return queryCurType;
    }

    public void setQueryCurType(String queryCurType) {
        this.queryCurType = queryCurType;
    }

    public Map<String, String> getYuabaoItmesRevise() {
        return yuabaoItmesRevise;
    }

    public void setYuabaoItmesRevise(Map<String, String> yuabaoItmesRevise) {
        this.yuabaoItmesRevise = yuabaoItmesRevise;
    }

    public Map<String, String> getRmbItmesRevise() {
        return rmbItmesRevise;
    }

    public void setRmbItmesRevise(Map<String, String> rmbItmesRevise) {
        this.rmbItmesRevise = rmbItmesRevise;
    }

    public Map<String, String> getYuabaoItmes() {
        return yuabaoItmes;
    }

    public void setYuabaoItmes(Map<String, String> yuabaoItmes) {
        this.yuabaoItmes = yuabaoItmes;
    }

    public List<EconomyGameManager.YuanbaoSumData> getYuanbaoSumDatas() {
        return yuanbaoSumDatas;
    }

    public void setYuanbaoSumDatas(List<EconomyGameManager.YuanbaoSumData> yuanbaoSumDatas) {
        this.yuanbaoSumDatas = yuanbaoSumDatas;
    }

    public ChartUIData getYuanbaoSumChart() {
        return yuanbaoSumChart;
    }

    public void setYuanbaoSumChart(ChartUIData yuanbaoSumChart) {
        this.yuanbaoSumChart = yuanbaoSumChart;
    }

    public List<YuanbaoConsumeData> getShangchengConsumeDatas() {
        return shangchengConsumeDatas;
    }

    public void setShangchengConsumeDatas(List<YuanbaoConsumeData> shangchengConsumeDatas) {
        this.shangchengConsumeDatas = shangchengConsumeDatas;
    }

    public ChartUIData getShangchengConsumeChart() {
        return shangchengConsumeChart;
    }

    public void setShangchengConsumeChart(ChartUIData shangchengConsumeChart) {
        this.shangchengConsumeChart = shangchengConsumeChart;
    }
}
